library(knitr)
knitr::opts_chunk$set(cache=T, comment=NA)
par(mar = c(1, 1, 1, 1))


## ----eval=FALSE----------------------------------------------------------
install.packages('statnet')
statnet::update_statnet()
## ------------------------------------------------------------------------
library(statnet)

help("ergm-terms")

## ------------------------------------------------------------------------
sessionInfo()

## ------------------------------------------------------------------------
set.seed(0)
library("sna")

survey.id <- as.matrix(Idai)
snag.id <- network(survey.id)
str(snag.id)
snag.id$names <- c("Canada", "Commission", "FAO", "IOM", "Italy", "Japan", "Mozambique", "Norway", "OCHA", "Oxfam", "Portugal", "Red Cross", "Save Child", "Sweden", "UAE", "UK", "UNFPA", "UNICEF", "USA", "WFP", "WHO", "World Bank", "WVI")
node.size.id <- setNames(c(0.692, 0.943, 0.783, 1.128, 1.192, 0.5, 1.515, 0.885, 1.452, 0.635, 0.746, 0.942, 0.974, 0.615, 0.1, 1.146, 1.02, 1.18, 1.032, 1.368, 1.234, 0.702,  0.905), c("Canada", "Commission", "FAO", "IOM", "Italy", "Japan", "Mozambique", "Norway", "OCHA", "Oxfam", "Portugal", "Red Cross", "Save Child", "Sweden", "UAE", "UK", "UNFPA", "UNICEF", "USA", "WFP", "WHO", "World Bank", "WVI"))
gplot(snag.id, usearrows = T, 
      label = snag.id$names, 
      label.pos = 9, 
      label.cex = .55, 
      label.col = "black",
      vertex.sides = c(3, 3, 50, 50, 3, 3, 3, 3, 50, 5, 3, 5, 5, 3, 3, 3, 50, 50, 3, 50, 50, 50, 5),
      vertex.col = c ( 3, 3, 50, 50, 3, 3, 3, 3, 50, 5, 3, 5, 5, 3, 3, 3, 50, 50, 3, 50, 50, 50, 5),
      edge.lwd = 0.00001,
      vertex.cex = 1.5,
      arrowhead.cex = 0.15, 
      mode = "fruchtermanreingold")

## ------------------------------------------------------------------------
gden(snag.id, mode = "digraph")
degree(snag.id, gmode = "digraph", cmode = "indegree")
degree(snag.id, gmode = "digraph", cmode = "outdegree")
closeness(snag.id, gmode = "digraph")
betweenness(snag.id, gmode = "digraph")

snag.degcentraliz <- centralization(snag.id, FUN = degree, mode = "digraph", cmode="indegree", normalize = T)
snag.degcentraliz

snag.closecentraliz <- centralization(snag.id, FUN = closeness, mode = "digraph", normalize = T)
snag.closecentraliz

snag.betwencentraliz <- centralization(snag.id, FUN = betweenness, mode = "digraph", normalize = T)
snag.betwencentraliz

cc.snag.id <- clique.census(snag.id)
cc.snag.id

library(igraph)
snag.id.igraph <- graph_from_edgelist(survey.id, directed = TRUE)
snag.id.igraph
mean_distance(snag.id.igraph)
install.packages("remotes")
remotes::install_github("schochastics/igraphUtils", force = TRUE)
library(netUtils)
survey
core_periphery(snag.id.igraph, method = "rk1_dc", iter = 500)
## ------------------------------------------------------------------------

set.seed(0)
set.vertex.attribute(snag.id, "orgGov", value = c("Gov", "Gov", "IO", "IO", "Gov", "Gov", "Gov", "Gov", "IO", "NGO", "Gov", "NGO", "NGO", "Gov", "Gov", "Gov", "IO", "IO", "Gov", "IO", "IO", "IO", "NGO"))
par(mfrow=c(2,2))

snagmodel.1 <- ergm(snag.id ~edges + mutual + gwesp (0.5, fixed = TRUE) 
                    + nodeifactor ('orgGov')
                    + nodeofactor ('orgGov'))
summary(snagmodel.1)

mcmc.diagnostics(snagmodel.1)
snagmodel.1.gof <- gof(snagmodel.1)
plot(snagmodel.1.gof)


snagmodel.2 <- ergm(snag.id ~edges + mutual + dgwesp (0.5, fixed = TRUE) 
                    + nodemix ('orgGov', base =c(2)))
summary(snagmodel.2)

mcmc.diagnostics(snagmodel.2)
snagmodel.2.gof <- gof(snagmodel.2)
plot(snagmodel.2.gof)
